<template>
  <view class="container">
    <view class="example">
      <uni-forms ref="form" :model="user" labelWidth="80px">
        <uni-forms-item label="学校全称" name="name">
          <uni-easyinput v-model="user.name" placeholder="请输入学校全称"/>
        </uni-forms-item>
        <uni-forms-item label="负责人" name="responsiblePersonName">
          <uni-easyinput v-model="user.responsiblePersonName" placeholder="请输入姓名"/>
        </uni-forms-item>
        <uni-forms-item label="性别" name="sex">
          <uni-data-checkbox v-model="user.sex" :localdata="sexs"/>
        </uni-forms-item>
        <uni-forms-item label="职务" name="jobTitle">
          <uni-easyinput v-model="user.jobTitle" placeholder="请输入职务"/>
        </uni-forms-item>
        <uni-forms-item label="任现职时间" name="jobTime">
          <uni-datetime-picker type="date" :clear-icon="false" v-model="user.jobTime"/>
        </uni-forms-item>
        <uni-forms-item label="学校等级" name="level">
          <uni-data-checkbox v-model="user.level" :localdata="level"/>
        </uni-forms-item>
        <uni-forms-item label="手机号码" name="phone">
          <uni-easyinput v-model="user.phone" placeholder="请输入手机号码"/>
        </uni-forms-item>
      </uni-forms>
      <button type="primary" @click="submit" v-show="showSubmit">提交</button>
    </view>
  </view>
</template>

<script>
import {
  getToken,
  getUser,
  getInfoId,
  getInforoleKey,
  setInfoId,
} from '@/utils/auth';
import {
  addInfo as addSchoolInfo,
  getSchoolInfoByUserId,
  updateInfo as updateSchoolInfo
} from "@/api/jz/school/info"

export default {
  data() {
    return {
      showSubmit: true,
      user: {
        name: "",
        phone: "",
        identityCard: "",
        email: "",
        sex: "",
        photograph: "",
        dateOfBirth: "",
        nation: "",
        nativePlace: "",
        schoolTag: "",
        education: "",
        professionalTitle: "",
        companyName: "",
        partyGroupings: "",
        jobTitle: "",
        refresherCourseCode: "",
        userId: getUser(),
      },
      sexs: [{
        text: '男',
        value: 0
      }, {
        text: '女',
        value: 1
      }],
      level: [{
        text: '幼儿园',
        value: 1
      }, {
        text: '小学',
        value: 2
      }, {
        text: '初中',
        value: 3
      }, {
        text: '高中',
        value: 4
      }, {
        text: '成职校',
        value: 5
      }, {
        text: '直属单位',
        value: 6
      }],
      roleKey: getInforoleKey(),
      fileList: [],
      rules: {
        phone: {
          rules: [{
            required: true,
            errorMessage: '手机号码不能为空'
          }, {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            errorMessage: '请输入正确的手机号码'
          }]
        },
      }
    }
  },
  onLoad() {
    getSchoolInfoByUserId(getUser()).then(res => {
      if (res.code === 200 && res.data) {
        this.user = res.data
      }
    })
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  methods: {
    addSchoolInfoFun(data) {
      addSchoolInfo(data).then(response => {
        if (response.code === 200) {
          this.$modal.msgSuccess("新增成功")
          setInfoId(response.data.id)
          uni.navigateBack({
            delta: 1
          })
        }
      })
    },
    updateSchoolInfoFun(data) {
      updateSchoolInfo(data).then(response => {
        if (response.code === 200) {
          this.$modal.msgSuccess("修改成功")
          uni.navigateBack({
            delta: 1
          })
        }
      })
    },
    // maskClick(e) {
    // },
    // getStudentInfo(id) {
    //   getStuInfo(id).then(response => {
    //     if (response.code == 200) {
    //       this.user = response.data
    //       this.fileList.push({
    //         url: this.user.photograph
    //       })
    //     }
    //
    //   })
    // },
    submit(ref) {
      this.showSubmit = false;
      this.$refs.form.validate().then(res => {
        if (this.user.id) {
          this.updateSchoolInfoFun(this.user)
        } else {
          this.addSchoolInfoFun(this.user)
        }
      })
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #ffffff;
}

.example {
  padding: 15px;
  background-color: #fff;
}

.segmented-control {
  margin-bottom: 15px;
}

.button-group {
  margin-top: 15px;
  display: flex;
  justify-content: space-around;
}

.form-item {
  display: flex;
  align-items: center;
  flex: 1;
}

.button {
  display: flex;
  align-items: center;
  height: 35px;
  line-height: 35px;
  margin-left: 10px;
}
</style>